﻿using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {
	//reference variables
	public Vector3 currentTargetPosition;
	public Vector2 destinationGrid;
	public GridCell destinationGridCell;
	public GridCell currentGridCell = new GridCell();
	public Vector2 currentGridCellBoardPosition;

	//movement variables
	public float radius = .25f;
	public float speed = 1f;
	public AStar astar;
	public bool isMoving;
	public bool breakCurrentPath;
	public bool isInTransit;

	Vector3 movingDirection = new Vector3(0,0,0);
	public GridCell[] myPath;
	public int pathIndex = 0;
	GridCell pathNode;
	//reference to the animator object
	public Animator anim;

	int maxPathLength = 0;
	//reference to the camera
	public Camera camera;

	// Use this for initialization
	void Start () {
		destinationGrid = new Vector2(-999,-999);
		currentTargetPosition = new Vector3(0f,0f,0f);
		currentGridCellBoardPosition = new Vector2(0,9);
		myPath = new GridCell[1000];
		anim = GetComponent<Animator>();
		GameController.gc.lineRenderer.SetColors(Color.red, Color.red);
		GameController.gc.lineRenderer.SetWidth(0.2F, 0.2F);
		GameController.gc.board[1,9] = currentGridCell;
	}

	void Update(){
		drawPath ();
	}
	
	// Update is called once per frame
	void FixedUpdate () {
		movePlayer();
	}

	public void movePlayer(){
		if(destinationGrid.x == -999){
			pathIndex = 0;
			return;
		}
		isMoving = true;
		//handle moving. update the animator variables as needed.
		if(myPath==null){
			isMoving = false;
			pathIndex = 0;
			anim.SetBool("isMoving", false);
			anim.SetBool("isMovingUp", false);
			anim.SetBool ("isMovingDown", false);
			anim.SetBool ("isMovingRight", false);
			anim.SetBool ("isMovingDown", false);
			return;

		}
		if(myPath[pathIndex]==null){
			pathIndex = 0;
			isMoving = false;
			anim.SetBool("isMoving", false);
			anim.SetBool("isMovingUp", false);
			anim.SetBool ("isMovingDown", false);
			anim.SetBool ("isMovingRight", false);
			anim.SetBool ("isMovingDown", false);
			clearPath();
		}
		else{
			anim.SetBool("isMoving", true);
			pathNode = myPath[pathIndex];
			currentTargetPosition = new Vector3(pathNode.transform.position.x, pathNode.transform.position.y, transform.position.z);
			movingDirection = (currentTargetPosition - transform.position);
			movingDirection.Normalize();
			isInTransit = true;
			performMove();
		}


	}
	//function for performing the move
	public void performMove(){
		if((currentTargetPosition - transform.position).magnitude > radius){
			transform.position += movingDirection * speed * Time.deltaTime;
			animateMove();
		}
		else{
			currentGridCell = GameController.gc.board[(int)pathNode.boardPosition.x,(int)pathNode.boardPosition.y];
			currentGridCellBoardPosition = currentGridCell.boardPosition;
			isInTransit = false;
			pathIndex++;
		}
	}
	//print out the path you should take. Debugging function.
	public void printPath(GridCell[] path){
		foreach(GridCell pathNode in path){
			if(pathNode!=null){
				Debug.Log (pathNode.boardPosition.x+ ", " + pathNode.boardPosition.y);
			}
		}
	}
	//clear out the path variable to ready for the next ones use.
	public void clearPath(){
		if(myPath!=null){
			for(int i = 0; i < 1000; i++){
				myPath[i] = null;
			}
		}
		pathIndex = 0;
	}
	//animates movement.
	public void animateMove(){
		if(movingDirection.x > .5f){
			anim.SetBool ("isMovingRight", true);
			anim.SetBool("isMovingUp", false);
			anim.SetBool ("isMovingDown", false);
			anim.SetBool ("isMovingLeft", false);
		}
		else if(movingDirection.x < -.5f){
			anim.SetBool("isMovingLeft", true);
			anim.SetBool("isMovingUp", false);
			anim.SetBool ("isMovingDown", false);
			anim.SetBool ("isMovingRight", false);
		}
		else if(movingDirection.y > .95f){
			anim.SetBool("isMovingUp", true);
			anim.SetBool ("isMovingDown", false);
			anim.SetBool ("isMovingRight", false);
			anim.SetBool ("isMovingLeft", false);
		}
		else if(movingDirection.y < -.95f){
			anim.SetBool("isMovingDown", true);
			anim.SetBool("isMovingUp", false);
			anim.SetBool ("isMovingRight", false);
			anim.SetBool ("isMovingLeft", false);
		}
	}
	//draws the path using the line renderer attached to the gamecontroller.
	void drawPath(){
		if(myPath!=null && GameController.gc.showPath){
			int j = 0;
			for(int i = 0; i < myPath.Length; i++){
				if(myPath[i]==null)
					break;
				j++;
			}
			GameController.gc.lineRenderer.SetVertexCount(0);
			GameController.gc.lineRenderer.SetVertexCount(j);
			int k = 0;
			for(int i = 0; i < myPath.Length; i++){
				if(i == myPath.Length || myPath[i] == null)
					break;
				GameController.gc.lineRenderer.SetPosition(i,new Vector3(myPath[i].transform.position.x, myPath[i].transform.position.y, -1));
				k++;
			}
			maxPathLength = Mathf.Max(j, maxPathLength);
			GameController.gc.lineRenderer.SetVertexCount(k);
		}
	}
}
